package org.spring.springboot.app.adv.advcommunityaround.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.adv.advcommunityaround.service.IAdvCommunityAroundService;
import org.spring.springboot.app.adv.advexam.service.IAdvExamService;
import org.spring.springboot.app.adv.advinfo.dao.AdvInfoDao;
import org.spring.springboot.app.adv.advinfo.domain.po.AdvInfoPO;
import org.spring.springboot.app.adv.advcommunityaround.dao.AdvCommunityAroundDao;
import org.spring.springboot.app.adv.advcommunityaround.domain.po.AdvCommunityAroundPO;
import org.spring.springboot.app.adv.advcommunityaround.domain.vo.*;
import org.spring.springboot.app.base.Constants;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdvCommunityAroundServiceImpl implements IAdvCommunityAroundService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdvCommunityAroundServiceImpl.class);

    @Autowired
    private AdvCommunityAroundDao advCommunityAroundDao;
    @Autowired
    private AdvInfoDao advInfoDao;
    @Autowired
    private IAdvExamService advExamService;


    @Override
    public AdvCommunityAroundPO find(String id) {
        return advCommunityAroundDao.selectByPrimaryKey(id);
    }

    @Override
    public R delete(String id) {
        R r = new R();
        AdvInfoPO mainPo = new AdvInfoPO();
        mainPo.setId(id);
        mainPo.preDelete();
        if (advInfoDao.updateByPrimaryKeySelective(mainPo) == 0) {
            return r.fail().setMessage("保存失败");
        }
        AdvCommunityAroundPO po = new AdvCommunityAroundPO();
        po.setId(id);
        po.preDelete();
        if (advCommunityAroundDao.updateByPrimaryKeySelective(po) == 0) {
            return r.fail().setMessage("保存失败");
        }
        return r.success();
    }


    @Override
    public List<AdvCommunityAroundPO> findPage(AdvCommunityAroundSearchVO searchVO, int pageNum, int pageSize) {
        return advCommunityAroundDao.findPage(searchVO, pageNum,pageSize);
    }

    @Override
    @Transactional
    public R saveInfo(AdvCommunityAroundSaveInVO vo, User user) {
        R r = new R();
        //保存主表
        AdvInfoPO mainPO = new AdvInfoPO();
        mainPO.setOfficeId(user.getOfficeId());
        mainPO.setType(Constants.ADV_INFO_TYPE_2);
        mainPO.setIsSystem(0);
        mainPO.setTitle(vo.getTitle());
        mainPO.setPhotos(vo.getPhotos());
        mainPO.setStartDate(vo.getStartDate_());
        mainPO.setEndDate(vo.getEndDate_());
        mainPO.setPayState(Constants.ADV_INFO_TYPE_PAY_STATE_1);
        mainPO.setPayMo(null);
        mainPO.setContent(vo.getContent());
        mainPO.preInsert();
        if (advInfoDao.insertSelective(mainPO) <1) {
            throw new RuntimeException("保存活动主表失败");
        }
        //保存副表
        AdvCommunityAroundPO CommunityAroundPO = new AdvCommunityAroundPO();
        CommunityAroundPO.setLongitude(0d);
        CommunityAroundPO.setLatitude(0d);
        CommunityAroundPO.setUrl(vo.getUrl());
        CommunityAroundPO.setAddress(null);
        CommunityAroundPO.setRecommend(null);
        CommunityAroundPO.preInsert();
        CommunityAroundPO.setId(mainPO.getId());
        if (advCommunityAroundDao.insertSelective(CommunityAroundPO) <1) {
            throw new RuntimeException("保存活动副表失败");
        }
        AdvCommunityAroundSaveOutVO outVO = new AdvCommunityAroundSaveOutVO();
        outVO.setId(mainPO.getId());
        return r.success().setData(outVO);
    }




    @Override
    public R updateInfo(AdvCommunityAroundUpdateInVO vo) {
        R r = new R();
        //判断广告是否审核（审核之前可以修改）
        AdvCommunityAroundPO CommunityAroundPO = advCommunityAroundDao.selectByPrimaryKey(vo.getId());
        if (CommunityAroundPO.getExamState() != null && CommunityAroundPO.getExamState().intValue() == Constants.ADV_EXAM_STATE_2) {
            return r.fail().setMessage("已审核广告不能修改");
        }

        //保存主表
        AdvInfoPO mainPo = new AdvInfoPO();
        mainPo.setId(vo.getId());
        mainPo.setTitle(vo.getTitle());
        mainPo.setContent(vo.getContent());
        mainPo.setPhotos(vo.getPhotos());
        mainPo.preUpdate();
        if (advInfoDao.updateByPrimaryKeySelective(mainPo) <1) {
            return r.fail().setMessage("修改活动主表失败");
        }
        AdvCommunityAroundPO CommunityAroundPO1 = new AdvCommunityAroundPO();
        CommunityAroundPO1.setId(vo.getId());
        CommunityAroundPO.setLongitude(0d);
        CommunityAroundPO.setLatitude(0d);
        CommunityAroundPO.setUrl(vo.getUrl());
        CommunityAroundPO.setAddress(null);
        CommunityAroundPO.setRecommend(null);
        CommunityAroundPO1.preUpdate();
        if (advCommunityAroundDao.updateByPrimaryKeySelective(CommunityAroundPO1) <1) {
            return r.fail().setMessage("修改活动副表失败");
        }
        return r.success();
    }


    @Override
    @Transactional
    public R pay(String id, User user) {
        R r = new R();
        AdvInfoPO mainPO = advInfoDao.selectByPrimaryKey(id);
        //判断支付状态
        if (mainPO.getPayState().intValue() == Constants.ADV_INFO_TYPE_PAY_STATE_2) {
            return r.fail().setMessage("活动已经支付");
        }
        //添加审核表
        advExamService.addExam(user.getOfficeId(),id);
        //修改广告状态
        mainPO.setPayState(Constants.ADV_INFO_TYPE_PAY_STATE_2);
        mainPO.preUpdate();
        if (advInfoDao.updateByPrimaryKeySelective(mainPO) < 1) {
            throw new RuntimeException("更新活动主表失败");
        }
        return r.success();
    }

    @Override
    public R saveSystemInfo(AdvCommunityAroundSystemSaveInVO vo, User user) {
        R r = new R();
        //保存主表
        AdvInfoPO mainPO = new AdvInfoPO();
        mainPO.setOfficeId(user.getOfficeId());
        mainPO.setType(Constants.ADV_INFO_TYPE_2);
        mainPO.setIsSystem(1);
        mainPO.setTitle(vo.getTitle());
        mainPO.setPhotos(vo.getPhotos());
        mainPO.setStartDate(vo.getStartDate_());
        mainPO.setEndDate(vo.getEndDate_());
        mainPO.setPayState(Constants.ADV_INFO_TYPE_PAY_STATE_0);
        mainPO.setPayMo(null);
        mainPO.setContent(vo.getContent());
        mainPO.preInsert();
        if (advInfoDao.insertSelective(mainPO) <1) {
            throw new RuntimeException("保存活动主表失败");
        }
        //保存副表
        AdvCommunityAroundPO CommunityAroundPO = new AdvCommunityAroundPO();
        CommunityAroundPO.setLongitude(0d);
        CommunityAroundPO.setLatitude(0d);
        CommunityAroundPO.setUrl(vo.getUrl());
        CommunityAroundPO.setAddress(null);
        CommunityAroundPO.setRecommend(null);
        CommunityAroundPO.preInsert();
        CommunityAroundPO.setId(mainPO.getId());
        if (advCommunityAroundDao.insertSelective(CommunityAroundPO) <1) {
            throw new RuntimeException("保存活动副表失败");
        }
        return r.success();
    }


}
